package com.infore.airsupervise.biz.impl;

import android.text.TextUtils;

import com.infore.airsupervise.app.Constants;
import com.infore.airsupervise.app.MsgConst;
import com.infore.airsupervise.app.StatusConst;
import com.infore.airsupervise.bean.MissionE;
import com.infore.airsupervise.bean.MissionHandleE;
import com.infore.airsupervise.bean.PolluterLicenseE;
import com.infore.airsupervise.biz.MissionBiz;
import com.infore.airsupervise.http.BaseResponse;
import com.infore.airsupervise.http.HttpAPI;
import com.infore.airsupervise.http.HttpCallBack;
import com.infore.airsupervise.http.HttpUtil;
import com.infore.airsupervise.http.OnServerResponseListener;
import com.infore.airsupervise.utils.JSONUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.MultipartBody;
import retrofit2.Call;

/**
 * @features: 任务相关业务接口实现类
 * @author: WuTao
 * @date: 2017/4/10 09:09
 */
public class MissionBizImpl implements MissionBiz {
    @Override
    public Call<BaseResponse> sendMissionListRequest(
            String userId, int missionType, String keyword, String startTime,String endTime,int state ,
                                                     final OnServerResponseListener<List<MissionE>> listener) {
        Map<String , Object> params = new HashMap<>();
        params.put(Constants.USER_ID_KEY,userId);
        params.put("type",missionType);
        if(!TextUtils.isEmpty(startTime)) {
            params.put("startTime",startTime);
        }
        if(!TextUtils.isEmpty(endTime)) {
            params.put("endTime", endTime);
        }
        params.put("keyword",keyword);
        if(state !=0) {
            params.put("state", state);
        }
        Call<BaseResponse> call = HttpUtil.getHttpService().postParams(HttpAPI.GET_MISSION_LIST,params);
        call.enqueue( new HttpCallBack(){
            @Override
            public void onSuccess(int operationCode, BaseResponse response) {
                super.onSuccess(operationCode, response);
                List<MissionE> eList = JSONUtil.parseList(response.getData(),Constants.RESPONSE_BEAN_LIST_KEY,MissionE.class);
                listener.onSucceed(eList,response.getMessage());
            }

            @Override
            public void onFailed(int operationCode, String desc) {
                super.onFailed(operationCode, desc);
                listener.onFailed(operationCode, MsgConst.GET_DATA_FAILED, desc);
            }

            @Override
            public void onException(int errorCode, String title, String desc) {
                super.onException(errorCode, title, desc);
                listener.onFailed(errorCode, title, desc);
            }
        });
        return call;
    }

    @Override
    public Call<BaseResponse> sendIssueMissionListRequest(String userId, int missionType, int state, final OnServerResponseListener<List<MissionE>> listener) {
        Map<String , Object> params = new HashMap<>();
        params.put(Constants.USER_ID_KEY,userId);
        params.put("missionType",missionType);
        params.put("state",state);
        final Call<BaseResponse> call = HttpUtil.getHttpService().postParams(HttpAPI.GET_MISSION_ISSUE_LIST,params);
        call.enqueue(new HttpCallBack(){
            public void onSuccess(int operationCode, BaseResponse response) {
                super.onSuccess(operationCode, response);
                List<MissionE> eList = JSONUtil.parseList(response.getData(),Constants.RESPONSE_BEAN_LIST_KEY,MissionE.class);
                listener.onSucceed(eList,response.getMessage());
            }

            @Override
            public void onFailed(int operationCode, String desc) {
                super.onFailed(operationCode, desc);
                listener.onFailed(operationCode, MsgConst.GET_DATA_FAILED, desc);
            }

            @Override
            public void onException(int errorCode, String title, String desc) {
                super.onException(errorCode, title, desc);
                listener.onFailed(errorCode, title, desc);
            }
        });
        return call;
    }

    @Override
    public Call<BaseResponse> sendMissionCommitRequest(String mn, String issuePsonId, MissionE missionE, final OnServerResponseListener<BaseResponse> listener) {
        Map<String , Object> params = new HashMap<>();
        params.put("title",missionE.getTitle());
        params.put("mn",mn);
        params.put("issuePsonId",issuePsonId);
        params.put("issueTime",missionE.getIssueTime());
        params.put("lastTime",missionE.getLastTime());
        params.put("handlePsonId",missionE.getHandlePsonId());
        params.put("discrib",missionE.getDiscrib());
        params.put("isApproved",missionE.getIsApproved());
        if(missionE.getIsApproved()== 1 ) {
            params.put("approvePson", missionE.getApprovePson());
        }
        params.put("leaderId",missionE.getLeaderId());
        List<MultipartBody.Part> files_part = new ArrayList<>();
        List<MultipartBody.Part> param_part = HttpUtil.getInstance().paramsToBodyParts(params);
        List<MultipartBody.Part> file_part = HttpUtil.getInstance().filesToBodyParts("file",missionE.getFile());
        List<MultipartBody.Part> voice_part = HttpUtil.getInstance().filesToBodyParts("voice",missionE.getVoice());
        files_part.addAll(file_part);
        files_part.addAll(voice_part);
        Call<BaseResponse> call = HttpUtil.getHttpService().postFiles(HttpAPI.COMMIT_MISSION_ISSUE,files_part,param_part);
        call.enqueue( new HttpCallBack() {
            @Override
            public void onSuccess(int operationCode, BaseResponse response) {
                super.onSuccess(operationCode, response);
                listener.onSucceed(response,response.getMessage());
            }

            @Override
            public void onException(int errorCode, String title, String desc) {
                super.onException(errorCode, title, desc);
                listener.onFailed(errorCode, title, desc);
            }

            @Override
            public void onFailed(int operationCode, String desc) {
                super.onFailed(operationCode, desc);
                listener.onFailed(operationCode, MsgConst.OPERATION_FAILED, desc);
            }
        });
        return call;
    }

    @Override
    public Call<BaseResponse> sendMissionProcessRequest(String missionId, final OnServerResponseListener<MissionHandleE> listener) {
        Map<String ,Object> params = new HashMap<>();
        params.put(Constants.MISSION_ID_KEY,missionId);
        final Call<BaseResponse> call = HttpUtil.getHttpService().postParams(HttpAPI.COMMIT_MISSION_PROCESS,params);
        call.enqueue( new HttpCallBack(){
            @Override
            public void onSuccess(int operationCode, BaseResponse response) {
                super.onSuccess(operationCode, response);
                MissionHandleE handleE = JSONUtil.parseBean(response.getData(),Constants.RESPONSE_BEAN_KEY, MissionHandleE.class);
                listener.onSucceed(handleE,response.getMessage());
            }

            @Override
            public void onFailed(int operationCode, String desc) {
                super.onFailed(operationCode, desc);
                listener.onFailed(operationCode, MsgConst.GET_DATA_FAILED, desc);
            }

            @Override
            public void onException(int errorCode, String title, String desc) {
                super.onException(errorCode, title, desc);
                listener.onFailed(errorCode, title, desc);
            }
        });

        return call;
    }

    /**
     * 获取执法记录列表
     * @param mn
     * @param listener
     * @return
     */
    @Override
    public Call<BaseResponse> getLawRecordList(String mn, final OnServerResponseListener<List<MissionE>> listener) {
        // 判断网络是否可用
        if (!HttpUtil.getInstance().isNetworkOk()) {
            listener.onFailed(StatusConst.CODE_CONNECT_FAIL, MsgConst.CONNECT_WRONG, MsgConst.PLEASE_CHECK_NETWORK);
            return null;
        }

        // 参数
        Map<String, Object> parameters = new HashMap<>();
        parameters.put("mn", mn);

        // 请求
        Call<BaseResponse> call = HttpUtil.getHttpService().postParams(HttpAPI.GET_LAW_RECORD_BY_MN, parameters);
        call.enqueue(new HttpCallBack(){
            @Override
            public void onSuccess(int operationCode, BaseResponse response) {
                super.onSuccess(operationCode, response);
                List<MissionE> beanList = JSONUtil.parseList(
                        response.getData(), Constants.RESPONSE_BEAN_LIST_KEY, MissionE.class);
                listener.onSucceed(beanList, response.getMessage());
            }

            @Override
            public void onFailed(int operationCode, String desc) {
                super.onFailed(operationCode, desc);
                listener.onFailed(operationCode, MsgConst.GET_DATA_FAILED, desc);
            }

            @Override
            public void onException(int errorCode, String title, String desc) {
                super.onException(errorCode, title, desc);
                listener.onFailed(errorCode, title, desc);
            }
        });

        return call;
    }
}
